package com.eypg.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.eypg.pojo.User;
import com.eypg.service.UserService;
import com.eypg.util.StringUtil;
import com.eypg.util.Struts2Utils;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author 作者ryan_song E-mail:song316@gmai.com
 * @version 创建时间：2011-3-3 下午10:12:32
 * 类说明
 */
public class UserAction extends ActionSupport{
	private static final long serialVersionUID = -6356307819518359036L;
	
	private String userName;
	private String pwd;
	private String newPwd;
	
	@Autowired
	@Qualifier("userService")
	private UserService userService;
	
	public String pChangePwd(){
		return "changePwd";
	}
	
	public String changePwd(){
		if(StringUtil.isBlank(newPwd) || StringUtil.isBlank(pwd)){
			Struts2Utils.renderJson("{\"statusCode\":\"300\", \"message\":\"密码不能为空!\"}");
			return null;
		}
		userName = null;
		userName = (String)Struts2Utils.getSession().getAttribute("userName");
		if(StringUtil.isBlank(userName)){
			Struts2Utils.renderJson("\"statusCode\":\"301\", \"message\":\"未登录或登录超时,请重新登录!\"");
			return null;
		}
		List<User> uList = userService.queryUser(" from User u where u.userName = '" + userName + "' and u.userPwd = '" + pwd + "'");
		if(uList == null || uList.size() <= 0){
			Struts2Utils.renderJson("{\"statusCode\":\"300\", \"message\":\"用户不存在!\"}");
			return null;
		}
		userService.updateUser(" update User u set u.userPwd = '" + newPwd + "' where u.userName = '" + userName + "'");
		Struts2Utils.renderJson("{\"statusCode\":\"200\", \"message\":\"密码修改成功!\", \"navTabId\":\"\", \"forwardUrl\":\"\", \"callbackType\":\"closeCurrent\"}");
		return null;
	}
	
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getNewPwd() {
		return newPwd;
	}
	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}
}
